#region Using References

using System.Diagnostics.Contracts;
using Beaker.Services.FileSystem;
using Beaker.Services.FileSystem.Physical;
using Beaker.Services.FileSystem.Zip;
using Beaker.Services.Tasks;
using GalaSoft.MvvmLight.Messaging;
using FileSystemService = Beaker.Services.FileSystem.FileSystemService;

#endregion

namespace Beaker.Services.Locator
{
	/// <summary>
	/// A version of the <see cref="ManualServiceLocator"/> that is pre-initialized with all services available in this assembly.
	/// </summary>
	public sealed class PreInitializedServiceLocator
		: ManualServiceLocator
	{
		private PreInitializedServiceLocator()
		{
			RegisterSingletonService<IMessenger>( new Messenger() );
			RegisterSingletonService( TaskService.Default );
			RegisterSingletonService( FileSystemService.Default );
			RegisterService<IFileSystemServiceProvider>( "Physical", () => new FileSystemServiceProvider() );
			RegisterService<IFileSystemServiceProvider>( "Zip", () => new ZipFileSystemServiceProvider() );
		}

		/// <summary>
		/// Call to initialize a predefined service locator that pre-provides all services in this assembly
		/// </summary>
		/// <returns>A <see cref="ManualServiceLocator"/> where you can manually add more service to.</returns>
		public static ManualServiceLocator Initialize()
		{
			Contract.Ensures( Contract.Result<ManualServiceLocator>() != null );

			var locator = new PreInitializedServiceLocator();
			ServiceLocator.SetLocator( locator );

			return locator;
		}

		/// <summary>
		/// Gets the current pre-initialized service locator, if any
		/// </summary>
		public static PreInitializedServiceLocator Current
		{
			get { return ServiceLocator.Current as PreInitializedServiceLocator; }
		}
	}
}